<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="客户">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
        .table-tool-div-screen .layui-inline{
            margin: 0 10px 10px 0;
        }
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();
            ajaxSelects("${baseUrl}custom/customType/getAll.action",[
                {selectName:"customTypeId",head:"客户分类" },
                {selectName:"moveCustomTypeId" }]);

            ajaxSelect("${baseUrl}user/user/getAll.action","userId","无");
        });

        layui.use(['form', 'table'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                util = layui.util

            $(".close").click(function () {
                closeAll();
            });

            //处理属性 为 lay-active 的所有元素事件
            util.event('lay-active', {
                export: function(){
                    let data = table.checkStatus('table').data;


                }
            });

            renderTable();

            table.on('tool(table)', function (obj) {
                let data = obj.data;
                switch(obj.event){
                    case 'delete':
                        del(data);
                        break;
                    case 'edit':
                        openTab("供应商<"+data.name+">编辑","${baseUrl}custom/customEdit.action?id="+data.id);
                        break;
                };
            });

            table.on('toolbar(table)', function (obj) {
                let data = table.checkStatus('table').data;
                switch(obj.event){
                    case 'add':
                        openTab("新增商品","${baseUrl}custom/customNew.action.action");
                        break;
                    case 'batchDelete':
                        if (checkSelectNum(data)){
                            confirm("确认删除选中的客户?",function () {
                                tipLoad("处理中，请稍后");
                                post("${baseUrl}custom/custom/batchDelete.action",genIds(data),function (resp) {
                                    closeLoading();
                                    if (resp.code == 1) {
                                        closeAll();
                                        table.reload('table',{page: {curr: 1}});
                                    }  else if (resp.code == 3) {
                                        closeAll();
                                        table.reload('table',{page: {curr: 1}});
                                        processFailInfo("批量删除客户成功,以下客户不允许删除,原因如下",resp.data);
                                    } else {
                                        errTip(resp.msg);
                                    }
                                });
                            });
                        }
                        break;
                    case 'export':
                        confirm("确认导出选中的客户?",function () {
                            tipLoad("处理中...");
                            post("${baseUrl}custom/custom/export.action" + genParam(genSearch()), {},function (resp) {
                                if (resp.code == 1) {
                                    downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                                    setTimeout(function () {
                                        closeAll();
                                    },1000);
                                } else {
                                    closeLoading();
                                    tip(resp.msg);
                                }
                            });
                        });
                        break;
                    case 'import':
                        openTab("客户导入","${baseUrl}custom/customImport.action");
                        break;
                    case 'moveType':
                        if (checkSelectNum(table.checkStatus('table').data)) {
                            openMiddleDialogByTagId("移动至分类", $("#moveTypeDialog"));
                        }
                        break;
                };
            });


            form.on('submit(search)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
                return false;
            });

        });

        function renderTable() {
            layui.use(['openTable'], function () {
                var openTable = layui.openTable;
                openTable.render({
                    id:'table'
                    , elem: '#table'
                    ,toolbar: '#tableToolBar'
                    ,defaultToolbar:[]
                    , url: "${baseUrl}custom/custom/getList.action?time="+(new Date()).valueOf()
                    , limit: 10
                    , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                        //,curr: 5 //设定初始在第 5 页
                        ,groups: 5 //只显示 5 个连续页码
                        ,first: false //不显示首页
                        ,last: false //不显示尾页
                    }
                    , done: function(res, curr, count){
                        setTableLineCss("table",res.data, "status", 2, "font-gray-c2c2c2");
                    }
                    , cols: [[
                        {type:'checkbox'},
                        {width:70,title:"序号",totalRowText: '合计',templet:function (d) {
                            if (d.status == 2) {
                                return "<span class='layui-badge layui-bg-gray'>停用</span>";
                            } else if (d.def == true) {
                                return "<span class='layui-badge layui-bg-green'>默认</span>";
                            } else {
                                return d.LAY_INDEX;
                            }
                        }},
                        {field: 'no', width: 130, title: '编号',style:"font-size:12px;"},
                        {field: 'name', width: 150, title: '名称',style:"font-size:12px;"},
                        {width: 120, title: '应收欠款',style:"font-size:12px;",templet:function (d) {
                                return getPrice(parseFloat(d.waitIn)+parseFloat(d.initAmt));
                            }},
                        {field: 'contacts', width: 120, title: '联系人',style:"font-size:12px;"},
                        {field: 'phone', width: 120, title: '联系电话',style:"font-size:12px;"},
                        {width: 120, title: '分类',style:"font-size:12px;",templet:function (d) {
                                return d.customType.name;
                            }},
                        {width: 120, title: '门店',style:"font-size:12px;",templet:function (d) {
                                return d.store==null ? '':d.store.name;
                            }},
                        {field: 'remark', minWidth: 120, title: '备注',style:"font-size:12px;"},
                        {title: '操作', minWidth: 80, fixed:'right', templet: '#operBar'},
                    ]]

                    //注：展开的列Items
                    , openCols: [
                        {field: 'no', title: '编号'},
                        {field: 'name', title: '名称'},

                        {title: '应收欠款',templet: function(d){
                                return genOpenTableContent("应收欠款",getPrice(parseFloat(d.waitIn)+parseFloat(d.initAmt)));
                            }},
                        {title: '关联业务员',templet: function(d){
                                return genOpenTableContent("关联业务员",d.user == null ? "" : d.user.name);
                        }},
                        {field: 'unitTel',title: '单位电话',
                            templet: function(d){
                                return genOpenTableContent("单位电话",d.unitTel == null ? "" : d.unitTel);
                            }},
                        {field: 'contacts',title: '联系人',
                            templet: function(d){
                                return genOpenTableContent("联系人",d.contacts == null ? "" : d.contacts);
                            }},
                        {field: 'phone',title: '联系电话',
                            templet: function(d){
                                return genOpenTableContent("联系电话",d.phone == null ? "" : d.phone);
                            }},
                        {title: '分类',
                            templet: function(d){
                                return genOpenTableContent("分类",d.customType.name);
                            }
                        },
                        {title: '状态',
                            templet: function(d){
                                return genOpenTableContent("状态",d.status == '1' ? "启用":"停用");
                            }
                        },
                        {title: '默认',
                            templet: function(d){
                                return genOpenTableContent("默认",isTrue(d.def) ? "是":"否");
                            }
                        },
                        {field: 'wechat',title: '微信',
                            templet: function(d){
                                return genOpenTableContent("微信",d.wechat == null ? "" : d.wechat);
                            }
                        },
                        {field: 'qq',title: 'QQ',
                            templet: function(d){
                                return genOpenTableContent("QQ",d.qq == null ? "" : d.qq);
                            }},
                        {field: 'tax',title: '传真',
                            templet: function(d){
                                return genOpenTableContent("传真",d.tax == null ? "" : d.tax);
                            }},
                        {field: 'email',title: '邮件',
                            templet: function(d){
                                return genOpenTableContent("邮件",d.email == null ? "" : d.email);
                            }},
                        {field: 'code',title: '邮编',
                            templet: function(d){
                                return genOpenTableContent("邮编",d.code == null ? "" : d.code);
                            }},
                        {field: 'address',title: '地址',
                            templet: function(d){
                                return genOpenTableContent("地址",d.address == null ? "" : d.address);
                            }},
                        {field: 'remark', title: '备注',
                            templet: function(d){
                                return genOpenTableContent("备注",d.remark == null ? "" : d.remark);
                            }},
                        {title: '门店',templet:function (d) {
                                return genOpenTableContent("门店",d.store==null ? '':d.store.name);
                            }},
                    ]
                });
            });

        }

        function del(data) {
            layer.confirm('删除客户《'+data.name+'》?', {icon: 3, title:'删除客户'}, function(index){
                tipLoad("删除中..");
                post("${baseUrl}custom/custom/delete.action",{id:data.id}, function (resp) {
                    closeAll();
                    if (resp.code == 1){
                        tip("删除成功");
                        renderTable();
                    } else {
                        tip(resp.msg);
                    }
                });
            });
        }


        function genSearch() {
            let json = getFormJson($("#searchForm"));
            return json;
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">其他信息</label>
                    <div class="layui-input-inline">
                        <input type="text" id="search" name="search" placeholder="编号、名称、联系信息、备注" style="font-size: 12px;width: 200px;" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <a class="layui-btn" lay-submit="" lay-filter="search" id="btn-search">
                        <i class="layui-icon layui-icon-search"></i>
                    </a>
                    <a class="layui-btn" id="selectSearch">筛选</a>
                </div>

                <div class="table-tool-div-screen" status="close" style="display: none">
                    <div class="layui-inline">
                        <label class="layui-form-label">客户分类</label>
                        <div class="layui-input-inline">
                            <select name="customTypeId">
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">编号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="no" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">单位电话</label>
                        <div class="layui-input-inline">
                            <input type="text" name="unitTel" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-inline">
                            <input type="text" name="phone" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">联系人</label>
                        <div class="layui-input-inline">
                            <input type="text" name="contacts" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-inline">
                            <input type="text" name="remark" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">关联业务员</label>
                        <div class="layui-input-inline">
                            <select name="userId">
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                        <a class="layui-btn layui-btn-primary" id="clearSearch">清空所有条件</a>
                    </div>

                    <script>
                        $("#selectSearch").click(function () {
                            if ($(".table-tool-div-screen").attr("status")=="close") {
                                $(".table-tool-div-screen").css("display","block");
                                $(".table-tool-div-screen").attr("status","open");
                                $("#search").parent().parent().css("display","none");
                                $("#search").val("");
                                $("#btn-search").css("display","none");
                                $(this).html("收起");
                            } else {
                                $(".table-tool-div-screen").css("display","none");
                                $(".table-tool-div-screen").attr("status","close");
                                $("#search").parent().parent().css("display","inline-block");
                                $("#btn-search").css("display","inline-block");
                                $(this).html("筛选");
                            }
                        });

                        $("#clearSearch").click(function () {
                            $("#searchForm")[0].reset();
                        });
                    </script>
                </div>
            </div>
        </form>
        <hr/>

        <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>

        <script type="text/html" id="tableToolBar">
            <div class="layui-inline mb4">
                <a class="layui-btn" lay-event="add">新增</a>
            </div>

            <div class="layui-inline mb4">
                <html:drop-menu menu="导入" lay_active="batchOperation">
                    <a href="javascript:;" lay-event="import">导入</a>
                    <a href="javascript:;" lay-event="export">导出</a>
                </html:drop-menu>
            </div>

            <div class="layui-inline mb4">
                <html:drop-menu menu="批量操作" lay_active="batchOperation">
                    <a href="javascript:;" lay-event="moveType">移动至分类</a>
                    <a href="javascript:;" lay-event="batchDelete">删除</a>
                </html:drop-menu>
            </div>
        </script>

        <script type="text/html" id="operBar">
            <a class="layui-btn layui-btn-xs table-operator" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger table-operator" lay-event="delete">删除</a>
        </script>
    </div>
</div>
</body>
<div id="moveTypeDialog" class="myDialog">
    <div>
        <form class="layui-form" id="moveTypeForm">
            <select name="moveCustomTypeId">
            </select>
        </form>
        <hr>
        <button class="layui-btn" id="sureMoveType">确认</button>
        <button class="layui-btn layui-btn-primary close">取消</button>
    </div>

    <script>
        $("#sureMoveType").click(function () {
            tipLoad("处理中，请稍后");
            let data = getFormJson($("#moveTypeForm"));
            post("${baseUrl}custom/custom/batchChangeCustomType.action?customTypeId="+data.moveCustomTypeId,genIds(layui.table.checkStatus('table').data),function (resp) {
                closeLoading();
                if (resp.code == 1) {
                    closeAll();
                    renderTable();
                }  else {
                    tip(resp.msg);
                }
            });
        });
    </script>
</div>


<html:failInfoDialog/>
<html:loading/>
</html>